package com.wudimeng.syll.common;

import com.github.pagehelper.PageInfo;
import com.wudimeng.syll.constants.ResponseConstants;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 通用返回对象
 * !!禁止修改!!
 *
 * @author wuxianliang
 * @version 1.0
 * @date 2019/4/1
 */
public class ApiResponse<T> extends LinkedHashMap<String, Object> implements Serializable {

    /**
     * 序列化
     */
    private static final long serialVersionUID = 1L;

    /**
     * 日期格式化
     */
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 返回信息工具
     */
    private static MessageUtil message = new MessageUtil();

    /**
     * 构造函数
     */
    private ApiResponse() {
        super();
    }

    /**
     * 获取返回对象实例
     *
     * @return CommonResponse
     */
    public static ApiResponse instance() {
        return instance(ResponseConstants.API_STATUS_CODE_OK,
                ResponseConstants.SUCCESS_MESSAGE, Boolean.TRUE, StringUtils.EMPTY);
    }

    /**
     * 获取返回对象实例
     *
     * @return CommonResponse
     */
    private static ApiResponse instance(int statusCode, String message, boolean success, Object data) {
        // 创建实例
        ApiResponse instance = new ApiResponse();
        // 状态代码
        instance.setStatusCode(statusCode);
        // 系统时间
        instance.setCurrentTime(new Date());
        // 返回信息
        instance.setMessage(message);
        // 是否成功
        instance.setSuccess(success);
        // 返回数据
        instance.setData(data);
        // TOKEN过期时间
//        instance.setExpire((Long) BaseContextHandler.get(CommonConstants.JWT_KEY_EXPIRE));

        return instance;
    }

    /**
     * 获取返回对象实例
     *
     * @return CommonResponse
     */
    public static ApiResponse instance(int statusCode, String message, boolean success) {
        return instance(statusCode, message, success, null);
    }

    /**
     * 获取返回分页对象实例
     *
     * @return CommonResponse
     */
    public static ApiResponse instance(PageInfo pageInfo) {
        return instance(getPageData(pageInfo));
    }

    /**
     * 获取返回分页对象 + 额外对象 的实例
     *
     * @return CommonResponse
     */
    public static ApiResponse instance(PageInfo pageInfo, Object data) {
        Map map = new LinkedHashMap<>();
        map.put("list",getPageData(pageInfo));
        map.put("result",data);
        return instance(map);
    }

    /**
     * 获取返回分页对象实例
     *
     * @return CommonResponse
     */
    public static ApiResponse instance(long current, long size, List rows) {
        return instance(getPageData(current, size, rows));
    }

    /**
     * 获取返回对象实例
     *
     * @return CommonResponse
     */
    public static ApiResponse instance(Object data) {
        return instance(ResponseConstants.API_STATUS_CODE_OK,
                ResponseConstants.SUCCESS_MESSAGE, Boolean.TRUE, data);
    }

    /**
     * 获取返回成功对象实例
     *
     * @return CommonResponse
     */
    public static ApiResponse success(String key) {
        return instance(ResponseConstants.API_STATUS_CODE_OK,
                message.get(key), Boolean.TRUE, StringUtils.EMPTY);
    }

    /**
     * 获取返回成功对象实例
     *
     * @return CommonResponse
     */
    public static ApiResponse success(String key, Object data) {
        return instance(ResponseConstants.API_STATUS_CODE_OK,
                message.get(key), Boolean.TRUE, data);
    }

    /**
     * 获取返回失败对象实例
     *
     * @return CommonResponse
     */
    public static ApiResponse error(String key) {
        return instance(ResponseConstants.API_STATUS_CODE_ERROR,
                message.get(key), Boolean.FALSE, null);
    }

    /**
     * 获取返回失败对象实例
     *
     * @return CommonResponse
     */
    public static ApiResponse error(String key, Object data) {
        return instance(ResponseConstants.API_STATUS_CODE_ERROR,
                message.get(key), Boolean.FALSE, data);
    }

    /**
     * 获取登录失败对象实例
     *
     * @return CommonResponse
     */
    public static ApiResponse loginError() {
        return instance(ResponseConstants.API_AUTH_CODE_LOGIN_NO,
                message.get("common.login.fail"), Boolean.FALSE, "请先登录！");
    }

    public void setCurrentTime(Date date) {
        super.put(ResponseConstants.CURRENT_TIME, sdf.format(date));
        super.put(ResponseConstants.CURRENT_TIME_MILLIS, date.getTime());
    }

    public void setStatusCode(int statusCode) {
        super.put(ResponseConstants.STATUS_CODE, statusCode);
    }

    public void setMessage(String message) {
        super.put(ResponseConstants.MESSAGE, message);
    }

    public void setSuccess(boolean success) {
        super.put(ResponseConstants.SUCCESS, success);
    }

    public void setData(Object data) {
        super.put(ResponseConstants.DATA, data);
    }

    private void setExpire(Long expire) {
        super.put(ResponseConstants.EXPIRE, expire);
    }

    public Long getExpire() {
        return (Long) super.get(ResponseConstants.EXPIRE);
    }

    public int getStatusCode() {
        return (int) super.get(ResponseConstants.STATUS_CODE);
    }

    public String getMessage() {
        return (String) super.get(ResponseConstants.MESSAGE);
    }

    public boolean getSuccess() {
        return (boolean) super.get(ResponseConstants.SUCCESS);
    }

    public Object getData() {
        return super.get(ResponseConstants.DATA);
    }

    /**
     * 自定义属性返回
     *
     * @param key
     * @param value
     */
    public void addCustomAttribute(String key, Object value) {
        super.put(key, value);
    }

    @Override
    public Object put(String key, Object value) {
        return "该方法已被禁用.";
    }

    private static Map getPageData(PageInfo pageInfo) {
        Map data = new LinkedHashMap<>();
        data.put("pageSize", pageInfo.getPageSize()); //每页的数量
        data.put("size", pageInfo.getSize());  //当前页的数量
        data.put("result", pageInfo.getList());
        data.put("current", pageInfo.getPageNum());
        data.put("pageCount", pageInfo.getPages());
        data.put("total", pageInfo.getTotal());
        data.put("hasNextPage", pageInfo.isHasNextPage());
        data.put("hasPreviousPage", pageInfo.isHasPreviousPage());
        return data;
    }

    private static Map getPageData(long current, long size, List rows) {
        Map data = new LinkedHashMap<>();
        data.put("size", size);
        data.put("data", rows);
        data.put("current", current);
        return data;
    }
}
